package com.hanthink.system.service.impl.manhour;

import java.sql.Date;
import java.util.List;

import com.hanthink.common.core.domain.entity.SysDept;
import com.hanthink.common.exception.CustomException;
import com.hanthink.common.utils.StringUtils;
import com.hanthink.system.domain.pc.EmJobItem;
import com.hanthink.system.mapper.EmJobItemMapper;
import com.hanthink.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hanthink.system.mapper.manhour.ProductionPlanInShiftMapper;
import com.hanthink.system.domain.manhour.ProductionPlanInShift;
import com.hanthink.system.service.manhour.IProductionPlanInShiftService;

/**
 * 用工计划数据填报Service业务层处理
 * 
 * @author hanthink
 * @date 2021-12-06
 */
@Service
public class ProductionPlanInShiftServiceImpl implements IProductionPlanInShiftService 
{
    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private ProductionPlanInShiftMapper productionPlanInShiftMapper;

    /**
     * 查询用工计划数据填报
     * 
     * @param id 用工计划数据填报ID
     * @return 用工计划数据填报
     */
    @Override
    public ProductionPlanInShift selectProductionPlanInShiftById(Long id)
    {
        return productionPlanInShiftMapper.selectProductionPlanInShiftById(id);
    }


    @Override
    public ProductionPlanInShift selectProductionPlanInShiftByKey(ProductionPlanInShift PlanData)
    {
        return productionPlanInShiftMapper.selectProductionPlanInShiftByKey(PlanData);
    }

    /**
     * 查询用工计划数据填报列表
     * 
     * @param productionPlanInShift 用工计划数据填报
     * @return 用工计划数据填报
     */
    @Override
    public List<ProductionPlanInShift> selectProductionPlanInShiftList(ProductionPlanInShift productionPlanInShift)
    {
        return productionPlanInShiftMapper.selectProductionPlanInShiftList(productionPlanInShift);
    }

    /**
     * 新增用工计划数据填报
     * 
     * @param productionPlanInShift 用工计划数据填报
     * @return 结果
     */
    @Override
    public int insertProductionPlanInShift(ProductionPlanInShift productionPlanInShift)
    {
        return productionPlanInShiftMapper.insertProductionPlanInShift(productionPlanInShift);
    }

    /**
     * 修改用工计划数据填报
     * 
     * @param productionPlanInShift 用工计划数据填报
     * @return 结果
     */
    @Override
    public int updateProductionPlanInShift(ProductionPlanInShift productionPlanInShift)
    {
        return productionPlanInShiftMapper.updateProductionPlanInShift(productionPlanInShift);
    }

    /**
     * 批量删除用工计划数据填报
     * 
     * @param ids 需要删除的用工计划数据填报ID
     * @return 结果
     */
    @Override
    public int deleteProductionPlanInShiftByIds(Long[] ids)
    {
        return productionPlanInShiftMapper.deleteProductionPlanInShiftByIds(ids);
    }

    /**
     * 删除用工计划数据填报信息
     * 
     * @param id 用工计划数据填报ID
     * @return 结果
     */
    @Override
    public int deleteProductionPlanInShiftById(Long id)
    {
        return productionPlanInShiftMapper.deleteProductionPlanInShiftById(id);
    }

    /**
     * 导入数据
     *
     * @param PrdPlanItemList     数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName        操作用户
     * @return 结果
     */
    @Override
    public String importPlanitem(List<ProductionPlanInShift> PrdPlanItemList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(PrdPlanItemList) || PrdPlanItemList.size() == 0) {
            throw new CustomException("导入的实际用工数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        int currentRowNum = 0;
        for (ProductionPlanInShift prdPlanItem : PrdPlanItemList) {
            currentRowNum++;
            try {
                ProductionPlanInShift items = productionPlanInShiftMapper.selectProductionPlanInShiftByKey(prdPlanItem);//.getPlanworkdatetime(),prdPlanItem.getPshiftid(),prdPlanItem.getMachineid() ,prdPlanItem.getMaterialid()) ;
                if (StringUtils.isNull(items)) {
                    prdPlanItem.setCreateBy(operName);
                    //1.过去的数据是否能修改?(原则上过去计划数据不能修改）
                    //2.修改为可以覆盖数据

                    this.insertProductionPlanInShift(prdPlanItem);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、用工数据" + prdPlanItem.getPlanworkdatetime() + prdPlanItem.getPshiftid() + " 导入成功");
                } else if (isUpdateSupport) {
                    prdPlanItem.setUpdateBy(operName);
                   // prdPlanItem.setId(items.getId());
                    this.updateProductionPlanInShift(prdPlanItem);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、用工数据 " + prdPlanItem.getPlanworkdatetime() + prdPlanItem.getPshiftid()  + " 更新成功");
                    continue;
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、用工数据 " + prdPlanItem.getPlanworkdatetime() +  prdPlanItem.getPshiftid() + " 已存在");
                    continue;
                }
            }catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、用工数据" + prdPlanItem.getCompanyid() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new CustomException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }


}
